package com.ss.android.action.a.a;

import android.text.TextUtils;

import com.ss.android.account.Spipe;
import com.ss.android.account.model.SpipeUser;
import com.ss.android.common.AbsApiThread;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.model.ForumLink;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class Comment {
    public String mediaId;
    public String url;
    public String mediaName;
    public String mediaInfoJson;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
    public long commentId;
    public boolean formFriend;
    public String userName;
    public long createTime;
    public String text;
    public String avatarUrl;
    public boolean userVerified;
    public String verifiedReason;
    public long userId;
    public String userProfileUrl;
    public int diggCount;
    public int buryCount;
    public boolean userDigg;
    public boolean userBury;
    public String openUrl;
    public long groupId;
    public long itemId;
    public String additionalInfo;
    public ForumLink forumLink;
    public String t;
    public String itemKey;
    public SpipeUser spipeUser;
    public int replyCount;
    public int isPgcAuthor;
    public List commentList;
    public List authorBadge;

    public Comment() {

    }

    public static String a(Spipe arg3, String arg4, String arg5) {
        String v0;
        if (!android.support.design.a.isTextEmpty(arg4) || !android.support.design.a.isTextEmpty(arg5)) {
            StringBuilder v0_1 = new StringBuilder(" ");
            if (arg3.d("qq_weibo")) {
                v0_1.append("|| ");
            } else {
                v0_1.append("// ");
            }

            if (!android.support.design.a.isTextEmpty(arg4)) {
                v0_1.append("@").append(arg4).append(": ");
            }

            if (!android.support.design.a.isTextEmpty(arg5)) {
                v0_1.append(arg5);
            }

            v0 = v0_1.toString();
        } else {
            v0 = "";
        }

        return v0;
    }

    public final String a(Spipe arg3) {
        return a(arg3, this.userName, this.text);
    }

    public final JSONObject toAbstractJSON() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("comment_id", this.commentId);
            jsonObject.put("text", this.text);
            jsonObject.put("user_id", this.userId);
            jsonObject.put("user_name", this.userName);
            if (!TextUtils.isEmpty(this.mediaInfoJson)) {
                jsonObject.put("media_info", new JSONObject(this.mediaInfoJson));
            }
            jsonObject.put("avatar_url", this.avatarUrl);
        } catch (JSONException exception) {
            exception.printStackTrace();
        }

        return jsonObject;
    }

    public final void initWithJO(JSONObject jsonObject) throws JSONException {
        JSONObject v0_1;
        int index = 0;
        if (jsonObject.has("id")) {
            this.commentId = jsonObject.getLong("id");
        } else if (jsonObject.has("comment_id")) {
            this.commentId = jsonObject.getLong("comment_id");
        } else {
            throw new JSONException("must have comment id");
        }

        this.groupId = jsonObject.optLong("group_id");
        this.itemId = jsonObject.optLong("item_id");
        jsonObject.optInt("aggr_type");
        this.createTime = jsonObject.optLong("create_time");
        this.userName = jsonObject.optString("user_name");
        if (android.support.design.a.isTextEmpty(this.userName)) {
            this.userName = jsonObject.optString("screen_name");
        }

        this.isPgcAuthor = jsonObject.optInt("is_pgc_author", 0);
        this.text = jsonObject.getString("text");
        this.avatarUrl = jsonObject.optString("user_profile_image_url");
        if (android.support.design.a.isTextEmpty(this.avatarUrl)) {
            this.avatarUrl = jsonObject.optString("avatar_url");
        }

        jsonObject.optString("platform");
        this.userVerified = jsonObject.optBoolean("user_verified");
        this.verifiedReason = jsonObject.optString("verified_reason");
        this.userId = jsonObject.optLong("user_id");
        this.userProfileUrl = jsonObject.optString("user_profile_url");
        this.replyCount = jsonObject.optInt("reply_count");
        this.diggCount = jsonObject.optInt("digg_count");
        jsonObject.optInt("bury_count");
        this.openUrl = jsonObject.optString("open_url");
        this.userDigg = jsonObject.optInt("user_digg") > 0;
        jsonObject.optInt("user_bury");
        this.additionalInfo = jsonObject.optString("additional_info");
        this.forumLink = ForumLink.fromJO(jsonObject.optJSONObject("forum_link"));
        dateFormat.format(new Date(this.createTime * 1000));
        this.spipeUser = new SpipeUser(this.userId);
        if (jsonObject.has("is_blocked")) {
            this.spipeUser.setIsBlocked(AbsApiThread.optBoolean(jsonObject, "is_blocked", false));
        }

        if (jsonObject.has("is_blocking")) {
            this.spipeUser.setIsBlocking(AbsApiThread.optBoolean(jsonObject, "is_blocking", false));
        }

        if (jsonObject.has("reply_list")) {
            JSONArray replayJA = jsonObject.optJSONArray("reply_list");
            if (replayJA == null) {
            } else {
                int length = replayJA.length();
                this.commentList = new ArrayList(length);
                while (index < length) {
                    JSONObject replyJO = replayJA.optJSONObject(index);
                    Comment comment = new Comment();
                    try {
                        comment.initWithJO(replyJO);
                        this.commentList.add(comment);
                    } catch (JSONException jsonException) {
                        jsonException.printStackTrace();
                    }
                    ++index;
                }
            }
        }

        JSONObject mediaInfo = jsonObject.optJSONObject("media_info");
        if (mediaInfo != null) {
            this.mediaInfoJson = mediaInfo.toString();
            this.mediaName = mediaInfo.optString("name");
            this.mediaId = mediaInfo.optString("media_id");
            if (!android.support.design.a.isTextEmpty(this.mediaId)) {
                new StringBuilder("http://www.toutiao.com/m").append(this.mediaId).append("/");
            }
        }
        this.authorBadge = ImageInfo.optImageList(jsonObject, "author_badge", true);
    }
}

